/**
 * Created by sanfast6 on 2016/3/22.
 */
var video = document.getElementById("myVideo");
var vLength;
var sTime;
var eTime;
var cW = null;
var timeDrag = false;
var cVol = null;
//  display video duration when available
video.addEventListener("timeupdate", function () {
    //  Current time

    var vTime = video.currentTime;
    vLength = video.duration

    getTime(vLength,"total-time");
    getTime(vTime,"current-time");
    vLength = video.duration.toFixed(1);
    if(eTime && vTime.toFixed(0) == eTime) {
        video.pause();
    }
   // $(".current-time").text(cTime);
    updateProgress(video.currentTime, video.duration);

}, false);

video.addEventListener("play", function(){
    $(".video-wrap").hide();
    $(".btn-p-p").addClass("pause");
    var width = cW || "5%";
    if(sTime) {
        setTime(sTime);
    }
});

video.addEventListener("loadedmetadata", function () {
    vLength = video.duration;
    var cTime = video.currentTime;
    getTime(vLength,"total-time");
    getTime(cTime,"current-time");
}, false);

$(".btn-p-p").click(function() {
    $(".prog-bar-point").removeClass("video-start-point");
    var width = cW || "5%";
    if(!cW) {
        $(".prog-bar").css({
            width : width
        });
    }
    if(video.paused) {
        $(this).addClass("pause");
        video.play();
    } else {
        $(this).removeClass("pause");
        video.pause();
    }
    return false
});

$(".video-progress").mousedown(function(e) {
    timeDrag = true;
    setProgress(e.pageX);
});
$(".video-progress").mousemove(function(e) {
    if(timeDrag) {
        setProgress(e.pageX);
    }
});
$(".video-progress").mouseup(function(e) {
    if(timeDrag) {
        timeDrag = false;
        video.currentTime = setProgress(e.pageX);
        video.play();
    }
});
//结束控制
video.addEventListener("ended", function () {
    $(".btn-p-p").removeClass("pause");
})
//全屏video容器样式改变
$('.media-full').on('click', function() {
    $(".source-box").toggleClass("full-screen");
    $(this).toggleClass("cancel-full-screen");
});
//关闭恢复声音
$(".media-vol").click(function (e) {
    if($(e.target).hasClass("media-vol")) {
        $(this).toggleClass("closed-vol");
        if($(this).hasClass("closed-vol")) {
            cVol = video.volume;
            video.volume = 0;

            $(".cur-vol").hide();
            $(".cur-vol-point").hide();
        } else {
           video.volume = cVol;
            $(".cur-vol").show();
            $(".cur-vol-point").show();
        }
    }
})
/*video.addEventListener("webkitfullscreenchange", function (e) {
    e.cancelable = true;
    console.log(e);
})*/
//调节声音
$(".media-vol-control").mouseup(function(e) {
    e.stopPropagation();
    if($(".media-vol").hasClass("closed-vol")) {
        $(".media-vol").removeClass("closed-vol");
        $(".cur-vol").show();
        $(".cur-vol-point").show();
    }
    var height = e.pageY - $(this).offset().top;
    var nHeight = $(this).height() - height;
    var percent = nHeight / $(this).height() * 100;
    if(percent > 100) {
        percent = 100;
    }
    if(percent < 0) {
        percent = 0;
    }
    $(".cur-vol").css({
        height : percent + "%"
    });
    $(".cur-vol-point").css({
        bottom : percent - 2 + "%"
    });

    video.volume = percent / 100;
    percent = parseInt(percent) + "%"
    $(".cur-vol-percent").text(percent);
})
//设置开始时间
function setTime(time) {
    video.currentTime = 0;
    video.currentTime += time;
}
/*//加载进度条
var startBuffer = function() {
    var maxduration = video.duration;
    var currentBuffer = video.buffered.end(0);
    var percentage = 100 * currentBuffer / maxduration;
    $('.buffer-bar').css('width', percentage+'%');

    if(currentBuffer < maxduration) {
        setTimeout(startBuffer, 500);
    }
/*};*//*
setTimeout(startBuffer, 500);*/
//设置进度条
function setProgress(x){
    video.pause();
    pChange = true;
    $(".prog-bar-point").removeClass("video-start-point");
    var nWidth = x - $(".video-progress").offset().left;
    var percent = nWidth/$(".video-progress").width() * 100;
    if(percent > 100) {
        percent = 100;
    }
    if(percent <= 5) {
        percent = 5;
    }
    $(".prog-bar").css({
        width: percent + "%"
    });
    return video.duration * percent / 100
}
//播放时更新进度条
function updateProgress(ct,et) {
    cW = (100 * (ct / et));
    if(cW <= 5 ) {
        cW = "5%";
    } else {
        cW += "%";
    }
    $(".prog-bar").css({
        width : cW
    });
}
//获取时间
function getTime(time,cName) {
    var h = parseInt(time/3600);
    h < 10 ? h = "0" + h : h;
    var m = parseInt((time - h*3600)/60);
    m < 10 ? m = "0" + m : m;
    var s = parseInt(time-(h*3600 + m*60));
    s < 10 ? s = "0" + s : s;
    if(h != 0) {
        var nTime = h + ":" + m + ":" + s;
    } else {
        var nTime = m + ":" + s;
    }
    $('.' + cName).text(nTime); // global variable
}


//进入全屏
function requestFullScreen() {
    if (video.requestFullscreen) {
        video.requestFullscreen();
    } else if (video.mozRequestFullScreen) {
        console.log("moz-in")
        video.mozRequestFullScreen();
    } else if (video.webkitRequestFullScreen) {
        console.log("chrome-in")
        video.webkitRequestFullScreen();
    }
}
//退出全屏
function exitFullScreen() {
    if (video.exitFullscreen) {
        video.exitFullscreen();
    } else if (video.mozCancelFullScreen) {
        console.log("quit")
        video.mozCancelFullScreen();
    } else if (video.webkitCancelFullScreen) {
        video.webkitCancelFullScreen();
    }
}
console.log(video.controller);
